package com.dao;

import com.baidu.StudentClass;
import com.jdbc.JdbcTest;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudenCla extends JdbcTest {
    Connection connection;
    PreparedStatement preparedStatement;
    ResultSet resultSet;

    //查询
    public List checkCl() throws SQLException {
        connection = JdbcTest.getConnection();
        preparedStatement = connection.prepareStatement("select * from studentClass");
        resultSet = preparedStatement.executeQuery();
        List<StudentClass> userArrayList = new ArrayList();
        while (resultSet.next()) {
            userArrayList.add(new StudentClass(resultSet.getInt(1), resultSet.getString(2)));

        }
        return userArrayList;
    }

//    增加
    public void increase(Integer id, String name) throws SQLException {
        System.out.println(connection);
        System.out.println(preparedStatement);
        connection = JdbcTest.getConnection();
        preparedStatement= connection.prepareStatement("insert into studentClass(id,name) values (?,?)");
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        preparedStatement.executeUpdate();
    }

    //修改
    public void studenCla(Integer id, String name) throws SQLException {
        System.out.println(connection);
        System.out.println(preparedStatement);
        connection = JdbcTest.getConnection();
        preparedStatement = connection.prepareStatement("update studentClass set name=? where id=?");
        preparedStatement.setString(1,name);
        preparedStatement.setInt(2,id);
        preparedStatement.executeUpdate();
    }

    //删除
    public void deleteData(Integer id) throws SQLException {
        connection = JdbcTest.getConnection();
        preparedStatement = connection.prepareStatement("delete from studentClass where id=?");
        preparedStatement.setInt(1,id);
        preparedStatement.executeUpdate();
    }
}
